package com.ruoyi.business.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.business.base.JoBaseServiceImpl;
import com.ruoyi.business.core.domain.DivisionsCode;
import com.ruoyi.business.core.domain.vo.DivisionsCodeAllVo;
import com.ruoyi.business.core.mapper.DivisionsCodeMapper;
import com.ruoyi.business.core.service.DivisionsCodeService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.StringJoiner;

@Service
public class DivisionsCodeServiceImpl extends JoBaseServiceImpl<DivisionsCodeMapper, DivisionsCode> implements DivisionsCodeService
{
    private IPage<DivisionsCode> findByCode(IPage page, String sql)
    {
        return joSqlUtil.jsonToEntity(baseMapper.pageJson(page, sql), DivisionsCode.class);
    }

    @Override
    public DivisionsCode findByCode(String code)
    {
        LambdaQueryWrapper<DivisionsCode> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DivisionsCode::getCode, code);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public IPage<DivisionsCode> findByPCode(IPage iPage, DivisionsCode code)
    {
        StringJoiner sql = new StringJoiner(" ").add("""
                SELECT CODE
                	,
                	NAME,
                	p_code\s
                FROM
                	divisions_code dc\s
                WHERE
                	dc.code=dc.code
                """);

        sql.add(joSqlUtil.sqlIf("and dc.p_code = '#{pCode}'\s", code.getPCode()));
        sql.add(joSqlUtil.sqlIf("AND dc.CODE = '#{code}'", code.getCode()));

        String sqlStr = joSqlUtil.sqlVO(sql.toString(), code);
        return findByCode(iPage, sqlStr);
    }

    @Override
    public IPage<DivisionsCode> findProvincesByCode(IPage iPage, DivisionsCode code)
    {
        StringJoiner sql = new StringJoiner(" ").add("""
                SELECT CODE
                	,
                	NAME,
                	p_code\s
                FROM
                	divisions_provinces dc\s
                WHERE
                	dc.code=dc.code
                """);

        sql.add(joSqlUtil.sqlIf("and dc.p_code = '#{pCode}'\s", code.getPCode()));
        sql.add(joSqlUtil.sqlIf("AND dc.CODE = '#{code}'", code.getCode()));

        String sqlStr = joSqlUtil.sqlVO(sql.toString(), code);
        return findByCode(iPage, sqlStr);
    }

    @Override
    public IPage<DivisionsCode> findCitiesByCode(IPage iPage, DivisionsCode code)
    {
        StringJoiner sql = new StringJoiner(" ").add("""
                SELECT CODE
                	,
                	NAME,
                	p_code\s
                FROM
                	divisions_cities dc\s
                WHERE
                	dc.code=dc.code
                """);

        sql.add(joSqlUtil.sqlIf("and dc.p_code = '#{pCode}'\s", code.getPCode()));
        sql.add(joSqlUtil.sqlIf("AND dc.CODE = '#{code}'", code.getCode()));

        String sqlStr = joSqlUtil.sqlVO(sql.toString(), code);
        return findByCode(iPage, sqlStr);
    }

    @Override
    public IPage<DivisionsCode> findAreasByCode(IPage iPage, DivisionsCode code)
    {
        StringJoiner sql = new StringJoiner(" ").add("""
                SELECT CODE
                	,
                	NAME,
                	p_code\s
                FROM
                	divisions_areas dc\s
                WHERE
                	dc.code=dc.code
                """);

        sql.add(joSqlUtil.sqlIf("and dc.p_code = '#{pCode}'\s", code.getPCode()));
        sql.add(joSqlUtil.sqlIf("AND dc.CODE = '#{code}'", code.getCode()));

        String sqlStr = joSqlUtil.sqlVO(sql.toString(), code);
        return findByCode(iPage, sqlStr);
    }

    @Override
    public IPage<DivisionsCode> findStreetsByCode(IPage iPage, DivisionsCode code)
    {
        StringJoiner sql = new StringJoiner(" ").add("""
                SELECT CODE
                	,
                	NAME,
                	p_code\s
                FROM
                	divisions_streets dc\s
                WHERE
                	dc.code=dc.code
                """);

        sql.add(joSqlUtil.sqlIf("and dc.p_code = '#{pCode}'\s", code.getPCode()));
        sql.add(joSqlUtil.sqlIf("AND dc.CODE = '#{code}'", code.getCode()));

        String sqlStr = joSqlUtil.sqlVO(sql.toString(), code);
        return findByCode(iPage, sqlStr);
    }

    @Override
    public IPage<DivisionsCode> findVillagesByCode(IPage iPage, DivisionsCode code)
    {
        StringJoiner sql = new StringJoiner(" ").add("""
                SELECT CODE
                	,
                	NAME,
                	p_code\s
                FROM
                	divisions_villages dc\s
                WHERE
                	dc.code=dc.code
                """);

        sql.add(joSqlUtil.sqlIf("and dc.p_code = '#{pCode}'\s", code.getPCode()));
        sql.add(joSqlUtil.sqlIf("AND dc.CODE = '#{code}'", code.getCode()));

        String sqlStr = joSqlUtil.sqlVO(sql.toString(), code);
        return findByCode(iPage, sqlStr);
    }

    @Override
    public IPage<DivisionsCodeAllVo> findAllByCode(IPage iPage, DivisionsCode code)
    {
        StringJoiner sql = new StringJoiner(" ").add("""
                SELECT *
                FROM
                	divisions_code_all dc\s
                WHERE
                	dc.provinces_code=dc.provinces_code
                """);

        sql.add(joSqlUtil.sqlIf("""
                	 and ( dc.provinces_code = '#{code}'\s
                	OR dc.cities_code = '#{code}'
                	OR dc.areas_code = '#{code}'
                	OR dc.streets_code = '#{code}')
                """, code.getCode()));

        String sqlStr = joSqlUtil.sqlVO(sql.toString(), code);
        return joSqlUtil.jsonToEntity(baseMapper.pageJson(iPage, sqlStr), DivisionsCodeAllVo.class);
    }

    @Override
    public List<DivisionsCodeAllVo> findAllByCode(DivisionsCode code)
    {
        StringJoiner sql = new StringJoiner(" ").add("""
                SELECT *
                FROM
                	divisions_code_all dc\s
                WHERE
                	dc.provinces_code=dc.provinces_code
                """);

        sql.add(joSqlUtil.sqlIf("""
                	 and ( dc.provinces_code = '#{code}'\s
                	OR dc.cities_code = '#{code}'
                	OR dc.areas_code = '#{code}'
                	OR dc.streets_code = '#{code}')
                """, code.getCode()));

        String sqlStr = joSqlUtil.sqlVO(sql.toString(), code);
        return joSqlUtil.jsonToEntity(baseMapper.findJson(sqlStr), DivisionsCodeAllVo.class);
    }


}
